import request from "../request";

/**
 * 获取应用配置
 * @returns {Promise}
 */
export function getConfig() {
  return request.get("/config");
}

/**
 * 获取地区列表
 * @returns {Promise}
 */
export function getRegionList() {
  return request.get("/common/region");
}

/**
 * 文件上传
 * @param {String} filePath 文件路径
 * @param {Object} options 上传选项
 * @returns {Promise}
 */
export function uploadFile(filePath, options = {}) {
  return new Promise((resolve, reject) => {
    const { filename = "file", formData = {}, showLoading = true } = options;

    if (showLoading) {
      uni.showLoading({
        title: "上传中...",
        mask: true,
      });
    }

    uni.uploadFile({
      url: request.baseURL + "/common/upload",
      filePath,
      name: filename,
      formData,
      header: {
        Authorization: `Bearer ${uni.getStorageSync("token")}`,
      },
      success: (res) => {
        if (res.statusCode === 200) {
          try {
            const data = JSON.parse(res.data);
            resolve(data);
          } catch (e) {
            reject(new Error("上传失败"));
          }
        } else {
          reject(new Error("上传失败"));
        }
      },
      fail: () => {
        reject(new Error("上传失败"));
      },
      complete: () => {
        if (showLoading) {
          uni.hideLoading();
        }
      },
    });
  });
}

/**
 * 获取版本信息
 * @returns {Promise}
 */
export function getVersionInfo() {
  return request.get("/common/version");
}

/**
 * 反馈意见
 * @param {Object} data 反馈数据
 * @returns {Promise}
 */
export function feedback(data) {
  return request.post("/common/feedback", data);
}
